<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区 -->
        <el-card>
            <!-- 用户列表区域 -->
            <el-table :data="userList" border stripe> 
            <!-- 展开列 -->
            <el-table-column type="expand">
                <template slot-scope="scope">
                    <el-row v-for="item in scope.row.orderitems" :key="item.orderItemId">
                        <el-divider content-position="left">商品名称：{{item.goods.goodsName}}</el-divider>
                        <el-form label-position="left" inline class="table-expand">
                            <el-form-item label="商品名称:">
                                <span>{{item.goods.goodsName}}</span>
                            </el-form-item>
                            <el-form-item label="商品封面:">
                                <el-image :src="item.goods.goodsCoverImg" style="width: 100px; height: 100px" ></el-image>
                            </el-form-item>
                            <el-form-item label="商品简介:">
                                <span v-html="item.goods.goodsIntro"></span>
                            </el-form-item>
                            <el-form-item label="商品数量:">
                                <span>{{item.goodsCount}}</span>
                            </el-form-item>
                        </el-form>
                    </el-row>
                </template>
            </el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="订单编号" prop="orderNo"></el-table-column>
                <el-table-column label="订单总价" prop="total"></el-table-column>
                <el-table-column label="订单状态" prop="orderStatus">
                    <template slot-scope="scope" >
                        <template v-if="scope.row.orderStatus === 0"><el-tag type="info">未付款</el-tag></template>
                        <template v-if="scope.row.orderStatus === 1"><el-tag type="success">已付款</el-tag></template>
                    </template>
                </el-table-column>
                <el-table-column label="下单时间" prop="payTime"></el-table-column>
                <!-- 自定义操作 -->
                <el-table-column label="操作" width="180px">
                    <template slot-scope="scope" >
                        <!-- 修改按钮 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.orderId)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
                <!-- 修改用户对话框 -->
                <el-dialog
                    title="修改订单"
                    :visible.sync="editDialogVisible"
                    width="50%"
                    @close="editDialogClosed"
                    >
                    <el-form :model="editFrom" :rules="editFromRules" ref="editFromRef" label-width="70px" >
                        <el-form-item label="订单编号">
                            <el-input v-model="editFrom.orderNo" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="订单总价" prop="total">
                            <el-input v-model="editFrom.total"></el-input>
                        </el-form-item>
                        <el-form-item label="订单状态" prop="orderStatus">
                            <template>
                                <el-radio-group v-model="editFrom.orderStatus">
                                  <el-radio :label="1" >已付款</el-radio>
                                  <el-radio :label="0" >未付款</el-radio>
                                </el-radio-group>
                            </template>
                        </el-form-item>
                    </el-form>
                      <span slot="footer" class="dialog-footer">
                        <el-button @click="editDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="editUserInfo">确 定</el-button>
                      </span>
                </el-dialog>
        </el-card>
    </div>
</template>
<script>
export default {
    data() {
      return {
          //获取用户列表的参数对象
          queryInfo: {
              query: '',
              //当前页数
              pagenum: 1,
              //当前每页显示多少数据
              pagesize: 10
          },
          userList: [],
          total: 0,
          editDialogVisible: false,
          //查询到的用户信息对象
          editFrom: {},
          editFromRules: {
              total: [
                  { required: true, message: '请输入订单总价', trigger: 'blur'}
              ]
          }
      }   
    },
    created() {
        this.getUserList()
    },
    methods: {
        async getUserList() {
            const {data : res} = await this.$http.post('/api/order/getAllOrder',this.queryInfo)
            if(res.code !==200) {
                return this.$message.error('获取订单列表失败！')
            }
            this.userList = res.data.records
            this.total = res.data.total
            console.log(res)    
        },
        //监听pagesize改变event
        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            this.getUserList()
        },
        //监听页码值改变事件
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage
            this.getUserList()
        },
        //修改用户信息
        async showEditDialog(id) {
            const {data: res} = await this.$http.post('/api/order/getOneOrder',{orderId:id})
            if (res.code !==200) {
                return this.$message.error('查询订单数据失败')
            }
            this.editFrom = res.data
            this.editDialogVisible = true
        },
        //监听用户修改对话框的关闭时间
        editDialogClosed() {
            this.$refs.editFromRef.resetFields()
        },
        //修改用户信息并提交
        editUserInfo() {
            this.$refs.editFromRef.validate(async valid =>{
                if (!valid) {
                    return
                }
                //发起表单修改
                const {data: res} = await this.$http.put('/api/order/updateOrder',this.editFrom)
                if (res.code !== 200) {
                    return this.$message.error(res.msg)
                }
                //关闭对话框
                this.editDialogVisible = false
                //刷新数据列表
                this.getUserList()
                //提示修改成功
                return this.$message.success(res.msg)
            })
        }
    }
}
</script>

<style lang="less" scoped>
.table-expand .el-form-item {
    margin-right: 10px;
    margin-bottom: 10px;
    width: 50%;
}
</style>